classdef ImageObject< handle & matlab.mixin.Copyable
    
     properties 
% %         coordis =[];
        boxes = [];
        labels = {};
        circle = {};
        imageHeight;
        imageWidth;
        imagePath;
    end
    methods
         function obj = ImageObject(fileName) %返回值必须是一个obj
            json = jsondecode(fileread(fileName));
            
            obj.imageWidth = json.imageWidth;
            obj.imageHeight = json.imageHeight;
            obj.imagePath = json.imagePath;
            
            process_shapes(obj, json.shapes);
            
% %             return obj
         end
    
         function process_shapes(obj, shapes)
%              shapes = json.shapes;
             for i = 1 : length(shapes)
                 shape = shapes(i);
                 box_class = shape.label;
                 shape_type = shape.shape_type;
                 
                 if strcmp(shape_type, 'circle') == true
                     centre_x = shape.points(1, 1);
                     centre_y = shape.points(1, 2);
                     
                     xmax = shape.points(2, 1);
                     ymax = shape.points(2, 2);
                     
                     r = norm([centre_x - xmax, centre_y - ymax]);
                     obj.circle = [centre_x, centre_y, r];
                     continue
                 end
                 
                if strcmp(shape_type, 'rectangle') == false
                    disp(['未知shape type:', box_class]);                     
                end
                 
                xmin = shape.points(1, 1);
                ymin = shape.points(1, 2);
                xmax = shape.points(2, 1);
                ymax = shape.points(2, 2);
                
                obj.boxes = [obj.boxes; [xmin, ymin, xmax, ymax, xmax-xmin,ymax-ymin]];
                obj.labels = [obj.labels, box_class];
                 
             end
             
         end
        
    end
    
end